﻿using System;
using System.Collections.Generic;
using GalaSoft.MvvmLight;

using DaphneProfiBase;
using GameEngine;
using Tomers.WPF.Localization;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;

namespace DaphneForms.ViewModel
{
    /// <summary>
    /// New Game Form ViewModel
    /// </summary>
    public class NewGameFormVM : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the NewGameFormVM class.
        /// </summary>
        public NewGameFormVM()
        {
            // set configuration
            OnePlayerGame = true;
            TwoPlayerGame = false;
            WhiteTurn = true;
            BlackTurn = false;
            ClassicCheckers = true;
            SuicidalCheckers = false;
            Result = false;

            // prepare checkers types
            CheckersTypes = new List<CheckersType>();
            SelectedCheckers = null;
            foreach (var type in SupportedCheckers.Types)
            {
                CheckersType cType = new CheckersType();
                cType.GameType = type.CheckersType;
                cType.Title = LanguageContext.Instance.Dictionary.Translate<string>(type.CheckersNameKey, "Value");
                CheckersTypes.Add(cType);
            }

            // create buttons
            OKCommand = new RelayCommand(() =>
                {
                    Result = true;
                    CloseForm();
                },
                () =>
                {
                    return SelectedCheckers != null;
                });

            CancelCommand = new RelayCommand(() =>
                {
                    Result = false;
                    CloseForm();
                });
        }

        /// <summary>
        /// Button result
        /// </summary>
        public bool Result
        { get; set; }

        /// <summary>
        /// Returns the settings
        /// </summary>
        public NewGameFormSettings Settings
        {
            get
            {
                NewGameFormSettings s = new NewGameFormSettings();
                s.CheckersType = SelectedCheckers.GameType;
                s.OnePlayerGame = OnePlayerGame;
                s.WhiteTurn = WhiteTurn;
                s.Suicidal = SuicidalCheckers;

                return s;
            }
        }

        #region Data Binding

        public bool OnePlayerGame
        { get; set; }

        public bool TwoPlayerGame
        { get; set; }

        public bool WhiteTurn
        { get; set; }

        public bool BlackTurn
        { get; set; }

        public bool ClassicCheckers
        { get; set; }

        public bool SuicidalCheckers
        { get; set; }

        public List<CheckersType> CheckersTypes
        { get; set; }

        public CheckersType SelectedCheckers
        { get; set; }

        public RelayCommand OKCommand
        { get; set; }

        public RelayCommand CancelCommand
        { get; set; }

        #endregion

        /// <summary>
        /// Sends message to close the form
        /// </summary>
        private void CloseForm() 
        {
            Messenger.Default.Send<NotificationMessage>(new NotificationMessage(this, Notify.CloseForm));
        }


        public class CheckersType
        {
            public enumGameType GameType
            { get; set; }

            public string Title
            { get; set; }

            public override string ToString()
            {
                return Title;
            }
        }

        
    }
}